package utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.part;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;

import utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionProtocol;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionprotocolsPackage;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.BusinessMessageEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.BusinessMessageNameEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.ControlFlowSegment2EditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.ControlFlowSegmentControlFlowSegmentCompartment2EditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.ControlFlowSegmentControlFlowSegmentCompartmentEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.ControlFlowSegmentEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.ControlFlowSegmentOperator2EditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.ControlFlowSegmentOperatorEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.InteractionPathEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.InteractionPathInteractionPathCompartmentEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.InteractionPathNameEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.InteractionProtocolEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.Lifeline2EditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.ProtocolReference2EditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.ProtocolReferenceEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.ProtocolReferenceName2EditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.ProtocolReferenceNameEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.RoleEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.RoleNameEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.RoleRoleCompartmentEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.Termination2EditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.TerminationEditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.TerminationTerminationElement2EditPart;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.edit.parts.TerminationTerminationElementEditPart;

/**
 * This registry is used to determine which type of visual object should be
 * created for the corresponding Diagram, Node, ChildNode or Link represented
 * by a domain model object.
 * 
 * @generated
 */
public class UpcolbpipVisualIDRegistry {

	/**
	 * @generated
	 */
	private static final String DEBUG_KEY = UpcolbpipDiagramEditorPlugin
			.getInstance().getBundle().getSymbolicName()
			+ "/debug/visualID"; //$NON-NLS-1$

	/**
	 * @generated
	 */
	public static int getVisualID(View view) {
		if (view instanceof Diagram) {
			if (InteractionProtocolEditPart.MODEL_ID.equals(view.getType())) {
				return InteractionProtocolEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		return utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.part.UpcolbpipVisualIDRegistry
				.getVisualID(view.getType());
	}

	/**
	 * @generated
	 */
	public static String getModelID(View view) {
		View diagram = view.getDiagram();
		while (view != diagram) {
			EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
			if (annotation != null) {
				return (String) annotation.getDetails().get("modelID"); //$NON-NLS-1$
			}
			view = (View) view.eContainer();
		}
		return diagram != null ? diagram.getType() : null;
	}

	/**
	 * @generated
	 */
	public static int getVisualID(String type) {
		try {
			return Integer.parseInt(type);
		} catch (NumberFormatException e) {
			if (Boolean.TRUE.toString().equalsIgnoreCase(
					Platform.getDebugOption(DEBUG_KEY))) {
				UpcolbpipDiagramEditorPlugin.getInstance().logError(
						"Unable to parse view type as a visualID number: "
								+ type);
			}
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static String getType(int visualID) {
		return String.valueOf(visualID);
	}

	/**
	 * @generated
	 */
	public static int getDiagramVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		if (InteractionprotocolsPackage.eINSTANCE.getInteractionProtocol()
				.isSuperTypeOf(domainElement.eClass())
				&& isDiagram((InteractionProtocol) domainElement)) {
			return InteractionProtocolEditPart.VISUAL_ID;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static int getNodeVisualID(View containerView, EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		String containerModelID = utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.part.UpcolbpipVisualIDRegistry
				.getModelID(containerView);
		if (!InteractionProtocolEditPart.MODEL_ID.equals(containerModelID)) {
			return -1;
		}
		int containerVisualID;
		if (InteractionProtocolEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.part.UpcolbpipVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = InteractionProtocolEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		switch (containerVisualID) {
		case RoleRoleCompartmentEditPart.VISUAL_ID:
			if (InteractionprotocolsPackage.eINSTANCE.getLifeline()
					.isSuperTypeOf(domainElement.eClass())) {
				return Lifeline2EditPart.VISUAL_ID;
			}
			break;
		case ControlFlowSegmentControlFlowSegmentCompartmentEditPart.VISUAL_ID:
			if (InteractionprotocolsPackage.eINSTANCE.getInteractionPath()
					.isSuperTypeOf(domainElement.eClass())) {
				return InteractionPathEditPart.VISUAL_ID;
			}
			break;
		case InteractionPathInteractionPathCompartmentEditPart.VISUAL_ID:
			if (InteractionprotocolsPackage.eINSTANCE.getControlFlowSegment()
					.isSuperTypeOf(domainElement.eClass())) {
				return ControlFlowSegment2EditPart.VISUAL_ID;
			}
			if (InteractionprotocolsPackage.eINSTANCE.getProtocolReference()
					.isSuperTypeOf(domainElement.eClass())) {
				return ProtocolReference2EditPart.VISUAL_ID;
			}
			if (InteractionprotocolsPackage.eINSTANCE.getTermination()
					.isSuperTypeOf(domainElement.eClass())) {
				return Termination2EditPart.VISUAL_ID;
			}
			break;
		case ControlFlowSegmentControlFlowSegmentCompartment2EditPart.VISUAL_ID:
			if (InteractionprotocolsPackage.eINSTANCE.getInteractionPath()
					.isSuperTypeOf(domainElement.eClass())) {
				return InteractionPathEditPart.VISUAL_ID;
			}
			break;
		case InteractionProtocolEditPart.VISUAL_ID:
			if (InteractionprotocolsPackage.eINSTANCE.getRole().isSuperTypeOf(
					domainElement.eClass())) {
				return RoleEditPart.VISUAL_ID;
			}
			if (InteractionprotocolsPackage.eINSTANCE.getTermination()
					.isSuperTypeOf(domainElement.eClass())) {
				return TerminationEditPart.VISUAL_ID;
			}
			if (InteractionprotocolsPackage.eINSTANCE.getProtocolReference()
					.isSuperTypeOf(domainElement.eClass())) {
				return ProtocolReferenceEditPart.VISUAL_ID;
			}
			if (InteractionprotocolsPackage.eINSTANCE.getControlFlowSegment()
					.isSuperTypeOf(domainElement.eClass())) {
				return ControlFlowSegmentEditPart.VISUAL_ID;
			}
			break;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static boolean canCreateNode(View containerView, int nodeVisualID) {
		String containerModelID = utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.part.UpcolbpipVisualIDRegistry
				.getModelID(containerView);
		if (!InteractionProtocolEditPart.MODEL_ID.equals(containerModelID)) {
			return false;
		}
		int containerVisualID;
		if (InteractionProtocolEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.part.UpcolbpipVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = InteractionProtocolEditPart.VISUAL_ID;
			} else {
				return false;
			}
		}
		switch (containerVisualID) {
		case RoleEditPart.VISUAL_ID:
			if (RoleNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (RoleRoleCompartmentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case TerminationEditPart.VISUAL_ID:
			if (TerminationTerminationElementEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ProtocolReferenceEditPart.VISUAL_ID:
			if (ProtocolReferenceNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ControlFlowSegmentEditPart.VISUAL_ID:
			if (ControlFlowSegmentOperatorEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ControlFlowSegmentControlFlowSegmentCompartmentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case InteractionPathEditPart.VISUAL_ID:
			if (InteractionPathNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (InteractionPathInteractionPathCompartmentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ControlFlowSegment2EditPart.VISUAL_ID:
			if (ControlFlowSegmentOperator2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ControlFlowSegmentControlFlowSegmentCompartment2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ProtocolReference2EditPart.VISUAL_ID:
			if (ProtocolReferenceName2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case Termination2EditPart.VISUAL_ID:
			if (TerminationTerminationElement2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case RoleRoleCompartmentEditPart.VISUAL_ID:
			if (Lifeline2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ControlFlowSegmentControlFlowSegmentCompartmentEditPart.VISUAL_ID:
			if (InteractionPathEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case InteractionPathInteractionPathCompartmentEditPart.VISUAL_ID:
			if (ControlFlowSegment2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ProtocolReference2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (Termination2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ControlFlowSegmentControlFlowSegmentCompartment2EditPart.VISUAL_ID:
			if (InteractionPathEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case InteractionProtocolEditPart.VISUAL_ID:
			if (RoleEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (TerminationEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ProtocolReferenceEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ControlFlowSegmentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case BusinessMessageEditPart.VISUAL_ID:
			if (BusinessMessageNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		}
		return false;
	}

	/**
	 * @generated
	 */
	public static int getLinkWithClassVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		if (InteractionprotocolsPackage.eINSTANCE.getBusinessMessage()
				.isSuperTypeOf(domainElement.eClass())) {
			return BusinessMessageEditPart.VISUAL_ID;
		}
		return -1;
	}

	/**
	 * User can change implementation of this method to handle some specific
	 * situations not covered by default logic.
	 * 
	 * @generated
	 */
	private static boolean isDiagram(InteractionProtocol element) {
		return true;
	}

	/**
	 * @generated
	 */
	private static class JavaConstraints {

	}

}
